<?php
namespace App\Service\Xcx;

class IndexService extends BaseService
{
    //用户微信信息更新
    public static function wxinfo($openid)
    {
        $res = self::dbConn(self::$dbWx)->where('openid', $openid)->first();
        $upData['openid'] = $openid;
        $upData['uptime'] = toDate();
        $backData = [
            'openid' => $openid,
            'nickName' => $res ? $res->nickName : '',
            'avatarUrl' => $res ? $res->avatarUrl : '',
        ];
        if ($res) {
            self::dbConn(self::$dbWx)->where('openid', $openid)->update($upData);
            $backData['uid'] = $res->id;
        } else {
            $upData['addtime'] = toDate();
            $uid = self::dbConn(self::$dbWx)->insertGetId($upData);
            $backData['uid'] = $uid;
        }
        return self::toback(0, '', $backData);
    }

    //用户微信信息更新
    public static function wxModify($uid, $ary)
    {
        $res = self::dbConn(self::$dbWx)->where('id', $uid)->first();
        $upData['uptime'] = toDate();
        if ($ary['nick']) $upData['nickName'] = $ary['nick'];
        if ($ary['head']) $upData['avatarUrl'] = $ary['head'];
        if (!$res) {
            return self::toback(-2, '没有记录');
        }
        self::dbConn(self::$dbWx)->where('id', $uid)->update($upData);
        return self::toback();
    }

    //用户微信头像
    public static function wxHead($uid, $wxhead)
    {
        $res = self::dbConn(self::$dbWx)->where('id', $uid)->first();
        $upData['uptime'] = toDate();
        if (!$res) {
            return self::toback(-2, '没有记录');
        }
        self::dbConn(self::$dbWx)->where('id', $uid)->update(['avatarUrl' => $wxhead]);
        return self::toback();
    }

    //我的页面
    public static function my($uid)
    {
        $arys = [];
        return $arys;
    }

    //获取主菜单
    public static function bomMenus()
    {
        $res = self::dbConn(self::$dbXcxMenu)
            ->select('id', 'title', 'link', 'icon0', 'icon1')
            ->where('is_bom', 1)
            ->where('enable', 0)
            ->orderBy('sort', 'desc')
            ->get();
        if (!count($res)) return [];
        foreach ($res as &$v) {
            $v->icon0 = file_exists($v->icon0) ? env('DOMAIN') . '/'. $v->icon0 : '';
            $v->icon1 = file_exists($v->icon1) ? env('DOMAIN') . '/'. $v->icon1 : '';
        }
        return [
            'menus' => $res,
            'kd' => count($res)==5 ? '20%' : '33%',
        ];
    }

    //首页轮播图
    public static function banner($type = 0)
    {
        $db = self::dbConn(self::$dbBanner)
            ->select('id', 'img', 'link')
            ->where('del', 0);
        if ($type) $db = $db->where('type', $type);
        $res = $db->get();
        if (!count($res)) return [];
        foreach ($res as &$v) {
            $v->img = file_exists($v->img) ? env('DOMAIN') . '/'. $v->img : '';
        }
        return $res;
    }

    //首页分类
    public static function cateList($type = 0)
    {
        $db = self::dbConn(self::$dbCate)
            ->select('id', 'title')
            //->select('id', 'title', 'attr_str')
            ->where('del', 0);
        if ($type) $db = $db->where('type', $type);
        $res = $db->get();
        if (!count($res)) return [];
//        foreach ($res as &$v) {
//            //$v->img = file_exists($v->img) ? env('DOMAIN') . '/'. $v->img : '';
//            $v->attrs = $v->attr_str ? explode(',', $v->attr_str) : [];
//        }
        return $res;
    }

//    //表单子分类
//    public static function cateChild($pid)
//    {
//        $db = self::dbConn(self::$dbCate)
//            ->select('id', 'title', 'attr_str')
//            ->where('del', 0)
//            ->where('pid', $pid);
//        $res = $db->get();
//        if (!count($res)) return [];
//        $arys = [];
//        foreach ($res as &$v) {
//            $arys[] = [
//                'id' => $v->id, 'title' => $v->title,
//                'attrs' => $v->attr_str ? explode(',', $v->attr_str) : [],
//            ];
//        }
//        return $arys;
//    }

    //社区列表
    public static function shequList()
    {
        $ary[] = [
            'id' => 0, 'title' => '所有',
        ];
        $res = self::dbConn(self::$dbShequ)
            ->select('id', 'title')
            ->where('del', 0)
            ->get();
        if (!count($res)) {
            return self::toback(0, '', $ary);
        }
        foreach ($res as $v) {
            $ary[] = [
                'id' => $v->id, 'title' => $v->title,
            ];
        }
        return self::toback(0, '', $ary);
    }

    //扫码获取
    public static function codeOne($code)
    {
        $res = self::dbConn(self::$dbQr)
            ->where('code', $code)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '无效码');
        }
        return self::toback(0, '', $res);
    }

    //上传图片
    public static function imgUpload($uid, $link)
    {
        self::dbConn(self::$dbImgTmp)->insertGetId([
            'uid' => $uid, 'link' => $link, 'addtime' => toDate(),
        ]);
        return self::toback();
    }

    //我的统计
    public static function myTongji($uid)
    {
        $shopids = [];
        $us = self::dbConn(self::$dbUserShop)
            ->where('del', 0)
            ->where('uid',$uid)
            ->get();
        if (count($us)) {
            foreach ($us as $v) {
                if (!in_array($v->shop_id, $shopids)) $shopids[] = $v->shop_id;
            }
        }
        $gdb = self::dbConn(self::$dbYwGoods)->where('del', 0);
        if ($shopids) $gdb = $gdb->whereIn('shop_id', $shopids);
        $gtotal = $gdb->count();
        $fwdb = self::dbConn(self::$dbYwFuwu)->where('del', 0);
        if ($shopids) $fwdb = $fwdb->whereIn('shop_id', $shopids);
        $fwtotal = $fwdb->count();
        $atotal = self::dbConn(self::$dbAddress)
            ->where('del', 0)
            ->where('uid', $uid)
            ->count();
        $farys = [];
        $uOne = self::dbConn(self::$dbWx)->where('id', $uid)->first();
        $familys = self::dbConn(self::$dbWx)
            ->where('shequ_id', $uOne->shequ_id)
            ->where('door', $uOne->door)
            //->where('id', '<>', $uid)
            ->get();
        if (count($familys)) {
            foreach ($familys as $v) {
                $uCodeNum = self::dbConn(self::$dbQrlog)
                    ->groupBy('code')
                    ->where('uid', $v->id)
                    ->count();
                $farys[] = [
                    'uid' => $v->id, 'uname' => $v->nickName,
                    'codeNum' => $uCodeNum,
                ];
            }
        }
        //小店订单
        $glist = self::dbConn(self::$dbYwGorder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->skip(0)->take(4)
            ->orderBy('id', 'desc')
            ->get();
        $gArys = [];
        if (count($glist)) {
            foreach ($glist as $v) {
                $ginfo = json_decode($v->ginfo, true);
                $gArys[] = [
                    'title' => $ginfo['gname'],
                    'ispay' => $v->ispay,
                    'ispay' => $v->ispay,
                    'addtime' => $v->addtime,
                ];
            }
        }
        //服务订单
        $fwlist = self::dbConn(self::$dbYwFworder)
            ->where('del', 0)
            ->where('uid', $uid)
            ->skip(0)->take(4)
            ->orderBy('id', 'desc')
            ->get();
        $fwArys = [];
        if (count($fwlist)) {
            foreach ($fwlist as $v) {
                $fwinfo = json_decode($v->fwinfo, true);
                $fwArys[] = [
                    'title' => $fwinfo['title'],
                    'ispay' => $v->ispay,
                    'addtime' => $v->addtime,
                ];
            }
        }
        $ary = [
            'family' => $farys,
            'goodsTotal' => $gtotal,
            'fuwuTotal' => $fwtotal,
            'addressTotal' => $atotal,
            'orderShop' => 0, 'orderFuwu' => 0,
            'orderShopList' => $gArys,
            'orderFuwuList' => $fwArys,
        ];
        return self::toback(0, '', $ary);
    }
}